﻿using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using OA.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OA.Controllers
{
    public class MenuItemController : BaseController
    {
        public ActionResult Index()
        {
            return View();
        }


        public PartialViewResult viewUpdate(int? Id)
        {
            var viewModel = new Models.MenuItem();

            if (Id.HasValue)
            {
                viewModel = Context.MenuItems.Find(Id);
            }

            return PartialView(viewModel);
        }



        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult Read([DataSourceRequest] DataSourceRequest request)
        {
            var query = Context.MenuItems;

            return Json(query.ToDataSourceResult(request));
        }


        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult Update(Models.MenuItem entity)
        {
            if (entity.Id == 0)
            {
                Context.MenuItems.Add(entity);
            }
            else
            {
                var _e = Context.MenuItems.Find(entity.Id);
                if (_e != null)
                {
                    _e.Name = entity.Name;
                    _e.Sort = entity.Sort;
                    _e.Route = entity.Route;
                    _e.MenuId = entity.MenuId;
                }
            }

            try
            {
                Context.SaveChanges();
                return Json(Success("操作成功"));
            }
            catch (Exception ex)
            {
                return Json(Failed(ex.Message));
            }
        }

        /// <summary>
        ///  删除菜单
        /// </summary>
        /// <param name="Id">菜单Id</param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult Delete(int Id)
        {
            if (Id != 0)
            {
                var entity = Context.MenuItems.Find(Id);
                Context.MenuItems.Remove(entity);
            }

            try
            {
                Context.SaveChanges();
                return Json(Success("操作成功"));
            }
            catch (Exception ex)
            {
                return Json(Failed(ex.Message));
            }
        }
    }
}